package com.joysuch.wwyt.monitoringcenter.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.joysuch.wwyt.bp.bean.BpBaseUserBean;
import com.joysuch.wwyt.bp.entity.InspectExceptionAlarmMesage;
import com.joysuch.wwyt.bp.entity.dto.InspectExceptionAlarmMesageDto;
import com.joysuch.wwyt.bp.enums.AlarmSourceTypes;
import com.joysuch.wwyt.bp.mapper.InspectExceptionAlarmMesageMapper;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.DateTimeUtil;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.enums.DeleteFlag;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.mh.bean.AlarmSummaryBean;
import com.joysuch.wwyt.mh.bean.AlarmSummaryDto;
import com.joysuch.wwyt.mh.entity.MhAlarmMessage;
import com.joysuch.wwyt.mh.entity.dsl.QMhAlarmMessage;
import com.joysuch.wwyt.mh.repository.MhAlarmMessageDao;
import com.joysuch.wwyt.monitoringcenter.InspectionSearchBean;
import com.joysuch.wwyt.monitoringcenter.bean.*;
import com.joysuch.wwyt.monitoringcenter.service.AlarmCenterService;
import com.joysuch.wwyt.monitoringcenter.service.LocateServerService;
import com.joysuch.wwyt.monitoringcenter.service.MonitoringCenterService;
import com.joysuch.wwyt.risk.entity.RiskDistrict;
import com.joysuch.wwyt.risk.entity.RiskEvaluationAlarmMesage;
import com.joysuch.wwyt.risk.entity.dsl.QRiskEvaluationAlarmMesage;
import com.joysuch.wwyt.risk.repository.RiskDistrictDao;
import com.joysuch.wwyt.risk.repository.RiskEvaluationAlarmMesageDao;
import com.joysuch.wwyt.util.StringUtil;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import javafx.util.Pair;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author ZhangDong
 * @Date 2020/2/25 15:57
 */
@Slf4j
@Service
@Transactional
public class AlarmCenterServiceImpl implements AlarmCenterService {


    @Autowired
    private MhAlarmMessageDao mhAlarmMessageDao;
    @Autowired
    private RiskDistrictDao riskDistrictDao;
    @Autowired
    private LocateServerService locateServerService;

    @Autowired
    private RiskEvaluationAlarmMesageDao riskAlarmMessageDao;

    @Autowired
    private BaseUserDao baseUserDao;

    @Autowired
    private MonitoringCenterService monitoringCenterService;

    @Autowired
    private InspectExceptionAlarmMesageMapper inspectExceptionAlarmMesageMapper;


    @Override
    public ResultBean queryAlarmMesssage(AlarmCenterQueryBean queryBean) {
//        if (queryBean.getPageNumber() <= 0) {
//            queryBean.setPageNumber(1);
//        }
//        if (queryBean.getPageSize() <= 0) {
//            queryBean.setPageSize(10);
//        }
//        int fromIndex = (queryBean.getPageNumber() - 1) * queryBean.getPageSize();
//        int toIndex = fromIndex + queryBean.getPageSize();
//        List<AlarmCenterBean> totalList = unionAllAlarmMessage(queryBean);
//
//        List<AlarmCenterBean> list = totalList.subList(fromIndex, Math.min(toIndex, totalList.size()));
//        list = mappingData(list);
//        log.info(" Alarm message total list size: " + totalList.size());
//        return ResultBean.pageData(list, totalList.size());
        return ResultBean.defaultSuccessResult();
    }

    @Override
    public AlarmCenterHandlerBean getAlarmHandlerDetail(Long id, String module) {
        AlarmCenterHandlerBean handlerBean = new AlarmCenterHandlerBean();
        if ("重大危险源".equals(module)) {
            MhAlarmMessage one = mhAlarmMessageDao.getOne(id);
            handlerBean.setAlarmReason(one.getAlarmReason());
            handlerBean.setMsg(one.getRemark());
            handlerBean.setHandleBy(one.getHandleBy());
            if (one.getHandleTime() != null) {
                handlerBean.setHandleTime(DateTimeUtil.localDateTime2Date(one.getHandleTime()));
            }


        } else if ("风险分区".equals(module)) {
            RiskEvaluationAlarmMesage one = riskAlarmMessageDao.getOne(id);
            handlerBean.setAlarmReason(one.getAlarmReason());
            handlerBean.setMsg(one.getHandleRemark());
            handlerBean.setHandleBy(baseUserDao.getRealNameById(one.getHandleBy()));
            if (one.getUpdateTime() != null) {
                handlerBean.setHandleTime(one.getUpdateTime());
            }
        }
        return handlerBean;
    }

    @Override
    public ResultBean hardzardPage(Pageable pageable, AlarmCenterQueryBean queryBean) {
        Pair<List<AlarmCenterBean>, Long> pair = queryMhAlarms(queryBean, pageable);
        List<AlarmCenterBean> alarmCenterBeans = pair.getKey();
        mappingData(alarmCenterBeans);
        return ResultBean.pageData(alarmCenterBeans, pair.getValue());
    }

    @Override
    public ResultBean riskAreaPage(Pageable pageable, AlarmCenterQueryBean queryBean) {
        Pair<List<AlarmCenterBean>, Long> pair = queryRiskAlarms(queryBean, pageable);
        List<AlarmCenterBean> riskList = pair.getKey();
        mappingData(riskList);
        return ResultBean.pageData(riskList, pair.getValue());
    }

    @Override
    public ResultBean harzardCount() {
        int count = mhAlarmMessageDao.countByStateIn();
        return ResultBean.success(count);
    }

    @Override
    public ResultBean riskAreaCount() {
        QRiskEvaluationAlarmMesage qm = QRiskEvaluationAlarmMesage.riskEvaluationAlarmMesage;
        Predicate p = qm.state.eq(0);
        long count = riskAlarmMessageDao.count(p);
        return ResultBean.success(count);
    }

    @Override
    public ResultBean alarmSummary(Integer days) {
        LocalDate startDate = LocalDate.now().minusDays(days - 1);
        LocalDateTime startTime = LocalDateTime.of(startDate, LocalTime.of(0, 0, 0));
        //重大危险源
        List<MhAlarmMessage> alarmMessageList = mhAlarmMessageDao.findByCreateTimeAfter(startTime);
        List<AlarmSummaryBean> collect = alarmMessageList.stream().map(AlarmSummaryBean::new).collect(Collectors.toList());
        //风险分区
        List<RiskEvaluationAlarmMesage> riskMessageList = riskAlarmMessageDao.findByCreateTimeAfter(DateTimeUtil.localDateTime2Date(startTime));
        List<AlarmSummaryBean> collect1 = riskMessageList.stream().map(AlarmSummaryBean::new).collect(Collectors.toList());
        collect.addAll(collect1);
        collect.sort(Comparator.comparing(AlarmSummaryBean::getAlarmTime));
        Map<LocalDate, List<AlarmSummaryBean>> collect2 = collect.stream().collect(Collectors.groupingBy(AlarmSummaryBean::getAlarmTime));
        //补日期
        for (int i = 0; i < days; i++) {
            collect2.putIfAbsent(startTime.toLocalDate().plusDays(i), Lists.newArrayList());
        }
        TreeMap<LocalDate, List<AlarmSummaryBean>> localDateListTreeMap = new TreeMap<>(collect2);
        //总数
        List<Integer> totalAlarmList = Lists.newArrayList();
        //已解决
        List<Integer> handleList = Lists.newArrayList();
        //未解决
        List<Integer> unHandleList = Lists.newArrayList();
        //日期
        List<LocalDate> dateList = Lists.newArrayList();
        for (LocalDate key : localDateListTreeMap.keySet()) {
            if (key.isAfter(LocalDate.now())) {
                continue;
            }
            List<AlarmSummaryBean> alarmSummaryBeans = localDateListTreeMap.get(key);
            //已处理
            int handled = (int) alarmSummaryBeans.stream().filter(r -> r.getState() == 2).count();
            int total = alarmSummaryBeans.size();
            int unHandle = total - handled;
            totalAlarmList.add(total);
            handleList.add(handled);
            unHandleList.add(unHandle);
            dateList.add(key);
        }
        return ResultBean.wrapSuccessfulResult(new AlarmSummaryDto(totalAlarmList, handleList, unHandleList, dateList));
    }

    @Override
    public ResultBean todayAlarm() {
        LocalDateTime zeroTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0, 0));
        int count = mhAlarmMessageDao.countByCreateTimeAfter(zeroTime);
        int count1 = riskAlarmMessageDao.countByCreateTimeAfter(DateTimeUtil.localDateTime2Date(zeroTime));
        return ResultBean.wrapSuccessfulResult(count + count1);
    }

    // 对当前页数据列表进行数据填充
    private List<AlarmCenterBean> mappingData(List<AlarmCenterBean> list) {
        for (AlarmCenterBean alarm : list) {
            if ("风险分区".equals(alarm.getModule())) {
                if (!StringUtil.isEmpty(alarm.getAlarmArea())) {
                    Optional<RiskDistrict> riskDistrict = riskDistrictDao.findById(new Long(alarm.getAlarmArea()));
                    if (riskDistrict.isPresent()) {
                        alarm.setAlarmArea(riskDistrict.isPresent() ? riskDistrict.get().getName() : "");
                        StringBuilder builder = new StringBuilder();
                        //拼接消息内容
                        builder.append("\"").append(riskDistrict.get().getName()).append("\"").append(alarm.getAlarmContent());
                        alarm.setAlarmContent(builder.toString());
                    }
                }
                if (!StringUtil.isEmpty(alarm.getHandleBy()) && StringUtil.isNumeric(alarm.getHandleBy())) {
                    String userName = baseUserDao.getBaseUserNameById(new Long(alarm.getHandleBy()));
                    if (userName != null) {
                        alarm.setHandleBy(userName);
                    }

                }
                alarm.setAlarmType("风险等级提升");
            } else if ("重大危险源".equals(alarm.getModule())) {
                {
//                    Optional<MhAlarmMessage> mh = monitoringCenterService.findById(alarm.getId());
//                    if (mh.isPresent()) {
//                        //拼接消息内容: 检测点名称+ 报警内容
//                        String pointName = mh.get().getMhMonitoringPoint().getName();
//                        if (!StringUtil.isEmpty(pointName)) {
//                            alarm.setAlarmContent(pointName + ": " + alarm.getAlarmContent());
//                        }
//
//                    }
                }
            }

        }
        return list;
    }


    private List<AlarmCenterBean> unionAllAlarmMessage(AlarmCenterQueryBean queryBean) {
        List<AlarmCenterBean> list = queryMhAlarms(queryBean, null).getKey();
        List<AlarmCenterBean> riskList = queryRiskAlarms(queryBean, null).getKey();

        list.addAll(riskList);

        //人员在岗在位消息只有未处理的
//        if(queryBean.getState() == 0){
//            List<AlarmCenterBean> locateList = convertLocateList(locateServerService.alarmMessages());
//            list.addAll(locateList);
//        }

        //按时间排序
        Collections.sort(list, new Comparator<AlarmCenterBean>() {
            @Override
            public int compare(AlarmCenterBean o1, AlarmCenterBean o2) {
                if (o1.getAlarmTime() != null && o2.getAlarmTime() != null) {
                    return o2.getAlarmTime().compareTo(o1.getAlarmTime());
                } else if (o1.getAlarmTime() != null) {
                    return -1;
                } else if (o2.getAlarmTime() != null) {
                    return 1;
                }
                return 0;
            }
        });

        return list;

    }

    private Pair<List<AlarmCenterBean>, Long> queryMhAlarms(AlarmCenterQueryBean queryBean, Pageable pageable) {
        QMhAlarmMessage qMhAlarmMessage = QMhAlarmMessage.mhAlarmMessage;
        Predicate predicate = qMhAlarmMessage.isNotNull();
        predicate = queryBean.getType() == null ? predicate : ExpressionUtils.and(predicate, qMhAlarmMessage.mhMonitoringType.id.eq(queryBean.getType()));
        predicate = queryBean.getStartTime() == null ? predicate : ExpressionUtils.and(predicate, qMhAlarmMessage.
                alarmTime.between(DateTimeUtil.date2LocalDateTime(queryBean.getStartTime()), DateTimeUtil.date2LocalDateTime(queryBean.getEndTime())));
        //已处理
        if (queryBean.getState() !=null&&queryBean.getState() == 1) {
            predicate = ExpressionUtils.and(predicate, qMhAlarmMessage.state.eq(2));
        }
        //未处理，报警中+回到正常值
        else {
            predicate = ExpressionUtils.and(predicate, qMhAlarmMessage.state.in(0, 1));
        }
        pageable = PageDataUtils.addOrderByDesc(pageable, "id");
        Iterable<MhAlarmMessage> iterableList = mhAlarmMessageDao.findAll(predicate, pageable);
        long totalElements = ((Page<MhAlarmMessage>) iterableList).getTotalElements();
        List<MhAlarmMessage> list = new ArrayList<>();
        for (MhAlarmMessage it : iterableList) {
            MhAlarmMessage alarm = new MhAlarmMessage();
            BeanUtils.copyProperties(it, alarm);
            list.add(alarm);
        }
        List<AlarmCenterBean> alarmCenterBeans = convertMhAlarmList(list);
        return new Pair(alarmCenterBeans, totalElements);
    }

    private Pair<List<AlarmCenterBean>, Long> queryRiskAlarms(AlarmCenterQueryBean queryBean, Pageable pageable) {
        QRiskEvaluationAlarmMesage qMessage = QRiskEvaluationAlarmMesage.riskEvaluationAlarmMesage;
        Predicate predicate = qMessage.isNotNull();
        predicate = ExpressionUtils.and(predicate, qMessage.state.eq(queryBean.getState()));
//        if (queryBean.getState() == 1) {
//            predicate = ExpressionUtils.and(predicate, qMessage.handleBy.eq(Context.getCurrentUserId()));
//        }
        predicate = queryBean.getRiskDistrictId() == null ? predicate : ExpressionUtils.and(predicate, qMessage.riskDistrictId.eq(queryBean.getRiskDistrictId()));
        predicate = queryBean.getStartTime() == null ? predicate : ExpressionUtils.and(predicate, qMessage.createTime.between(queryBean.getStartTime(), queryBean.getEndTime()));
        pageable = PageDataUtils.addOrderByDesc(pageable, "id");
        Page<RiskEvaluationAlarmMesage> all = riskAlarmMessageDao.findAll(predicate, pageable);
        return new Pair(convertRiskAlarmList(all.getContent()), all.getTotalElements());
    }


    private List<AlarmCenterBean> convertLocateList(List<McAlarmMessageBean> locateList) {
        List<AlarmCenterBean> list = new ArrayList<>();
        for (McAlarmMessageBean mc : locateList) {
            AlarmCenterBean alarm = new AlarmCenterBean();
            alarm.setId(mc.getId());
            alarm.setAlarmContent(mc.getContent());
            alarm.setHandleStatus("未处理");
            alarm.setAlarmLevel("一键报警".equals(mc.getType()) ? "二级报警" : "一级报警");
            alarm.setAlarmType(mc.getType());
            try {
                alarm.setAlarmTime(DateTimeUtil.parseDateTime(mc.getTime()));
            } catch (ParseException e) {
                log.error("日期解析出错： " + mc.getTime());
            }

            alarm.setModule("人员在岗在位");
            list.add(alarm);
        }
        return list;
    }


    private List<AlarmCenterBean> convertMhAlarmList(List<MhAlarmMessage> mhList) {
        List<AlarmCenterBean> list = new ArrayList<>();
        if (mhList == null) {
            return list;
        }

        for (MhAlarmMessage m : mhList) {
            AlarmCenterBean alarm = new AlarmCenterBean();
            alarm.setId(m.getId());
            alarm.setAlarmContent(m.getDetail());
            alarm.setHandleStatus(m.getState() != 2 ? "未处理" : "已处理");
            alarm.setAlarmType(m.getMhMonitoringType().getName());
            if (m.getLevel() == 1 || m.getLevel() == 2) {
                alarm.setAlarmLevel("一级报警");
            } else {
                alarm.setAlarmLevel("二级报警");
            }
            if (m.getHandleTime() != null) {
                alarm.setHandleTime(DateTimeUtil.localDateTime2Date(m.getHandleTime()));
            }
            alarm.setAlarmArea(m.getAlarmArea());
            if (m.getCreateTime() != null) {
                alarm.setAlarmTime(DateTimeUtil.localDateTime2Date(m.getCreateTime()));
            }
            alarm.setHandleBy(m.getHandleBy());
            alarm.setModule("重大危险源");
            alarm.setAlarmContent(monitoringCenterService.extranctMhAlarmContent(m));
            alarm.setAlarmReason(m.getAlarmReason());
            alarm.setRemark(m.getRemark());
            list.add(alarm);
        }
        return list;
    }

    private List<AlarmCenterBean> convertRiskAlarmList(List<RiskEvaluationAlarmMesage> rList) {
        List<AlarmCenterBean> list = new ArrayList<>();
        if (rList == null) {
            return list;
        }
        for (RiskEvaluationAlarmMesage m : rList) {
            AlarmCenterBean alarm = new AlarmCenterBean();
            alarm.setId(m.getId());
            alarm.setAlarmContent(m.getDetail());
            alarm.setHandleStatus(m.getState() == 0 ? "未处理" : "已处理");
            // alarm.setAlarmLevel("二级报警");
            //alarm.setAlarmType("");
            if (m.getState() == 1) {
                alarm.setHandleTime(m.getUpdateTime());
            }
            if (m.getRiskDistrictId() != null) {
                alarm.setAlarmArea(m.getRiskDistrictId().toString());
            }
            if (m.getCreateTime() != null) {
                alarm.setAlarmTime(m.getCreateTime());
            }

            if (m.getState() == 1 && m.getHandleBy() != null) {
                alarm.setHandleBy(m.getHandleBy().toString());
            }
            alarm.setAlarmReason(m.getAlarmReason());
            alarm.setRemark(m.getHandleRemark());
            alarm.setModule("风险分区");
            list.add(alarm);
        }
        return list;
    }

    @Override
    public ResultBean inspectPage(InspectionSearchBean bean) {
        List<InspectExceptionAlarmMesageDto> resultList = null;
        Long currentTenantId = Context.getCurrentTenantId();
        String currentOrgCode = Context.getCurrentOrgCode();
        LambdaQueryWrapper<InspectExceptionAlarmMesage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InspectExceptionAlarmMesage::getDeleteFlag, DeleteFlag.EFFECTIVE.getCode())
                .eq(bean.getState() != null, InspectExceptionAlarmMesage::getState, bean.getState())
                .eq(bean.getAlarmSource() != null, InspectExceptionAlarmMesage::getMonitoringType, bean.getAlarmSource())
                .between(bean.getStartTime() != null && bean.getEndTime() != null, InspectExceptionAlarmMesage::getAlarmTime,
                        bean.getStartTime(), bean.getEndTime())
                .eq(InspectExceptionAlarmMesage::getTenentId, currentTenantId)
                .likeRight(InspectExceptionAlarmMesage::getOrgCode, currentOrgCode)
                .orderByDesc(InspectExceptionAlarmMesage::getAlarmTime);
        IPage<InspectExceptionAlarmMesage> alarmMesageIPage = inspectExceptionAlarmMesageMapper.selectPage(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(bean.getPage() + 1, bean.getSize()), wrapper);
        if (!CollectionUtils.isEmpty(alarmMesageIPage.getRecords())) {
            resultList = converDate(alarmMesageIPage);
        }
        return CollectionUtils.isEmpty(resultList) ? ResultBean.pageData(new ArrayList<>(), alarmMesageIPage.getTotal()) : ResultBean.pageData(resultList, alarmMesageIPage.getTotal());
    }

    @Override
    public Integer inspectCount(Long tenentId, String orgCode) {
        LambdaQueryWrapper<InspectExceptionAlarmMesage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InspectExceptionAlarmMesage::getDeleteFlag, DeleteFlag.EFFECTIVE.getCode())
                .eq(InspectExceptionAlarmMesage::getState, 0)
                .eq(InspectExceptionAlarmMesage::getTenentId, tenentId)
                .likeRight(InspectExceptionAlarmMesage::getOrgCode, orgCode);
        return inspectExceptionAlarmMesageMapper.selectCount(wrapper);
    }

    /**
     * 报警信息的转换
     *
     * @param alarmMesageIPage
     * @return
     */
    public List<InspectExceptionAlarmMesageDto> converDate(IPage<InspectExceptionAlarmMesage> alarmMesageIPage) {
        List<InspectExceptionAlarmMesageDto> mesageDtoList = new ArrayList<>();
        for (InspectExceptionAlarmMesage record : alarmMesageIPage.getRecords()) {
            InspectExceptionAlarmMesageDto exceptionAlarmMesageDto = new InspectExceptionAlarmMesageDto();
            BeanUtils.copyProperties(record, exceptionAlarmMesageDto);
            exceptionAlarmMesageDto.setAlarmContent(record.getDetail());
            converMonitoringTypeAndUserAndState(record, exceptionAlarmMesageDto);
            mesageDtoList.add(exceptionAlarmMesageDto);
        }
        return mesageDtoList;
    }

    /**
     * 转换报警类型
     *
     * @param message
     * @param dto
     */
    public void converMonitoringTypeAndUserAndState(InspectExceptionAlarmMesage message, InspectExceptionAlarmMesageDto dto) {
        dto.setModule("巡检排查");
        if (AlarmSourceTypes.OVERDUE_UNCHECKED.getCode().equals(message.getMonitoringType())) {
            dto.setAlarmType(AlarmSourceTypes.OVERDUE_UNCHECKED.getValue());
        } else if (AlarmSourceTypes.SIGN_IN_ABNORMAL.getCode().equals(message.getMonitoringType())) {
            dto.setAlarmType(AlarmSourceTypes.SIGN_IN_ABNORMAL.getValue());
        } else if (AlarmSourceTypes.CONTENT_ABNORMAL.getCode().equals(message.getMonitoringType())) {
            dto.setAlarmType(AlarmSourceTypes.CONTENT_ABNORMAL.getValue());
        }
        String inspectUserJson = message.getInspectUser();
        if (StringUtils.isNotBlank(inspectUserJson)) {
            List<String> names = new ArrayList<>();
            List<BpBaseUserBean> inspectUser = getBpBaseUserBeans(inspectUserJson);
            dto.setInspectUser(inspectUser);
            for (BpBaseUserBean bpBaseUserBean : inspectUser) {
                String realName = baseUserDao.getRealNameById(bpBaseUserBean.getId());
                names.add(realName);
            }
            dto.setUserNames(names);
        }
        if (message.getHandleBy() != null) {
            String handleByName = baseUserDao.getRealNameById(message.getHandleBy());
            dto.setHandleByName(handleByName);
        }
        if (message.getState() == 0) {
            dto.setHandleState("未处理");
        } else {
            dto.setHandleState("已处理");
        }
    }


    private List<BpBaseUserBean> getBpBaseUserBeans(String userJson) {
        List<Long> remindUserIdList = JSONArray.parseArray(userJson, Long.class);
        List<BpBaseUserBean> userBeanList = new ArrayList<>(remindUserIdList.size());
        if (!CollectionUtils.isEmpty(remindUserIdList)) {
            Map<Long, String> userMap = baseUserDao.findAllById(remindUserIdList).stream().collect(Collectors.toMap(r -> r.getId(), r -> r.getRealName()));
            remindUserIdList.forEach(r -> userBeanList.add(new BpBaseUserBean(r, userMap.getOrDefault(r, ""))));
        }
        return userBeanList;
    }


    @Override
    public Map<String, Object> alarmTrendStatistics(String startTime, String endTime) {
        if (StrUtil.isBlank(startTime) || StrUtil.isBlank(endTime)) {
            throw new IllegalArgumentException("开始时间/结束时间不能为空");
        }
        List<DateTime> dateTimes = DateUtil.rangeToList(DateUtil.parse(startTime), DateUtil.parse(endTime), DateField.DAY_OF_MONTH);
        List<Map<String, String>> mapList = new ArrayList<>();
        List<Map<String, String>> maps = mhAlarmMessageDao.alarmTrendStatistics(startTime, endTime);
        for (DateTime dateTime : dateTimes) {
            boolean isTrue = false;
            String day = DateUtil.format(dateTime, DatePattern.NORM_DATE_FORMAT);

            for (Map<String, String> map : maps) {
                if (map.get("day").equals(day)) {
                    mapList.add(map);
                    isTrue = true;
                    break;
                }
            }
            if (!isTrue) {
                Map<String, String> map = new HashMap<>();
                map.put("day", day);
                map.put("num", "0");
                mapList.add(map);
            }

        }
        return MapUtil.of("data", mapList);
    }
}
